﻿<UserControl xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
             xmlns:ViewModels="clr-namespace:BillsManager.ViewModels;assembly=BillsManager.ViewModels"
             xmlns:Caliburn="http://www.caliburnproject.org"
             xmlns:DataTemplates="clr-namespace:BillsManager.Views.DataTemplates"
             xmlns:Controls="clr-namespace:BillsManager.Views.Controls"
             xmlns:local="clr-namespace:BillsManager.Views"
             xmlns:Commanding="clr-namespace:BillsManager.ViewModels.Commanding;assembly=BillsManager.ViewModels"
             x:Name="userControl"
             x:Class="BillsManager.Views.DBsView"
             mc:Ignorable="d"
             d:DesignHeight="250"
             d:DesignWidth="700"
             d:DataContext="{d:DesignInstance IsDesignTimeCreatable=True, Type={x:Type ViewModels:DBsViewModel}}">

    <UserControl.Resources>
        <ResourceDictionary Source="Dictionaries\Styles.xaml" />
    </UserControl.Resources>

    <UserControl.Background>
        <Binding Mode="OneTime"
                 Source="{StaticResource ViewBackgroundBrush}" />
    </UserControl.Background>

    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="Auto" />
            <ColumnDefinition Width="{StaticResource RegionSeparatorSize}" />
            <ColumnDefinition />
        </Grid.ColumnDefinitions>

        <StackPanel Grid.Row="2">

            <TextBlock Text="Opened databases"
                       Margin="5 2"
                       Foreground="{StaticResource ViewTitleBrush}" />

            <Controls:ListBoxEx ItemsSource="{Binding Items}"
                                SelectedItem="{Binding SelectedDB}"
                                Margin="0 0 0 40"
                                Grid.IsSharedSizeScope="True">

                <Controls:ListBoxEx.ItemTemplate>
                    <DataTemplate>
                        <Border x:Name="border"
                                Margin="-5"
                                Padding="5">
                            <Grid>
                                <Grid.ColumnDefinitions>
                                    <ColumnDefinition Width="Auto"
                                                      SharedSizeGroup="col1" />
                                    <ColumnDefinition MinWidth="{StaticResource ListItemSpaceSize}" />
                                    <ColumnDefinition Width="Auto" />
                                </Grid.ColumnDefinitions>

                                <!--<StackPanel Orientation="Horizontal"
                                        VerticalAlignment="Center">-->
                                <TextBlock x:Name="txbName"
                                           Text="{Binding DisplayName}"
                                           VerticalAlignment="Center" />
                                <!--<TextBlock Text=" *"
                                           Visibility="{Binding IsDirty, Converter={StaticResource BooleanToVisibilityConverter}, Mode=OneWay}"
                                           VerticalAlignment="Center" />
                            </StackPanel>-->

                                <Button x:Name="btnCloseDB"
                                        Grid.Column="2"
                                        Padding="2"
                                        BorderThickness="0"
                                        VerticalAlignment="Stretch"
                                        Background="Transparent"
                                        BorderBrush="{x:Null}"
                                        Foreground="{Binding Foreground, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ListBoxItem}}}"
                                        Width="{Binding ActualHeight, Mode=OneWay, RelativeSource={RelativeSource Self}}"
                                        Command="{Binding DataContext.CloseDBCommand, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type local:DBsView}}}"
                                        CommandParameter="{Binding}"
                                        Focusable="False"
                                        VerticalContentAlignment="Stretch"
                                        HorizontalContentAlignment="Stretch">
                                    <Button.Content>
                                        <Path Stretch="Uniform"
                                              Data="{StaticResource CloseGeometry}"
                                              Fill="{Binding Foreground, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Button}}}" />
                                    </Button.Content>
                                </Button>

                            </Grid>
                        </Border>

                        <DataTemplate.Triggers>

                            <DataTrigger Binding="{Binding IsDirty}"
                                         Value="true">
                                <Setter TargetName="border"
                                        Property="Background"
                                        Value="{StaticResource WarningBackgroundBrush}" />
                                <Setter TargetName="txbName"
                                        Property="Foreground"
                                        Value="{StaticResource WarningForegroundBrush}" />
                                <Setter TargetName="btnCloseDB"
                                        Property="Foreground"
                                        Value="{StaticResource WarningForegroundBrush}" />
                            </DataTrigger>

                        </DataTemplate.Triggers>

                    </DataTemplate>
                </Controls:ListBoxEx.ItemTemplate>

            </Controls:ListBoxEx>

            <TextBlock Text="Available databases"
                       Margin="5 2"
                       Foreground="{StaticResource ViewTitleBrush}" />

            <Controls:ListBoxEx ItemsSource="{Binding NotOpenedDBs}"
                                SelectedItem="{Binding SelectedDB}"
                                Grid.IsSharedSizeScope="True"
                                Commanding:CommandBehavior.Event="MouseDoubleClick"
                                Commanding:CommandBehavior.Command="{Binding OpenDBCommand}"
                                Commanding:CommandBehavior.CommandParameter="{Binding SelectedDB}">

                <Controls:ListBoxEx.ItemTemplate>
                    <DataTemplate>
                        <Grid>
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="Auto"
                                                  SharedSizeGroup="col1" />
                                <ColumnDefinition MinWidth="{StaticResource ListItemSpaceSize}" />
                                <ColumnDefinition Width="Auto" />
                            </Grid.ColumnDefinitions>

                            <TextBlock Text="{Binding DisplayName}"
                                       HorizontalAlignment="Left"
                                       VerticalAlignment="Center" />

                            <Button Grid.Column="3"
                                    Padding="2"
                                    BorderThickness="0"
                                    VerticalAlignment="Stretch"
                                    Background="Transparent"
                                    BorderBrush="{x:Null}"
                                    Foreground="{Binding Foreground, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ListBoxItem}}}"
                                    Width="{Binding ActualHeight, Mode=OneWay, RelativeSource={RelativeSource Self}}"
                                    Command="{Binding DataContext.OpenDBCommand, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type local:DBsView}}}"
                                    CommandParameter="{Binding}"
                                    Focusable="False"
                                    VerticalContentAlignment="Stretch"
                                    HorizontalContentAlignment="Stretch">
                                <Button.Content>
                                    <Path Stretch="Uniform"
                                          Data="{StaticResource RightArrow2}"
                                          Fill="{Binding Foreground, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Button}}}" />
                                </Button.Content>
                            </Button>

                        </Grid>
                    </DataTemplate>
                </Controls:ListBoxEx.ItemTemplate>

            </Controls:ListBoxEx>
        </StackPanel>

        <ContentControl x:Name="ActiveItem"
                        Grid.Column="2" />

    </Grid>
</UserControl>